using System.Diagnostics;
using static SmartSite.Common.CheckingNullArgument;

namespace SmartSite.Frame
{
    public abstract class WithCommunicationIdFrame : BaseFrame
    {
        /// <summary>
        /// 通讯号. 3 Bytes. 由服务器分配. 紧邻 InfoLength 之后.
        /// </summary>
        protected IValueField<ulong> CommunicationIdField { get; }
        public long CommunicationId
        {
            get => (long)CommunicationIdField.Value;
            set => CommunicationIdField.Value = (ulong)value;
        }

        protected WithCommunicationIdFrame(byte[] buffer)
        : base(CheckingNull(buffer, nameof(buffer)))
        {
            CommunicationIdField = CreateCommunicationIdField();
            Debug.Assert(3 << 3 == (int)CommunicationIdField.Index);
        }

        ULongField CreateCommunicationIdField() => new ULongField(InfoLengthField, 24);

        protected ushort EvalInfoLength()
            => (ushort)(CRCField.Index - CommunicationIdField.EndIndex).ToTakeUpBytes();
    }
}
